﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Milkify.Interfaces;
using IronCow;
using System.Windows.Input;
using Milkify.Data;
using Milkify.Input;

namespace Milkify.Controllers
{
    class TagListController : TaskListListControllerBase
    {
        public TagListController(ControllerContext context, Rtm rtm, IViewContainer viewContainer, IViewFactory viewFactory)
            : base(context, rtm, viewContainer, viewFactory)
        {
            // We don't need to refresh the view on the first display because
            // we give the list of tasks in GetTagListViewData.
            View.NeedsRefresh = false;
        }

        protected override TaskListListViewData CreateViewData()
        {
            IEnumerable<TaskListViewData> taskLists = GetTagListViewData();
            var viewData = new TaskListListViewData(taskLists)
            {
                Title = "Tags"
            };
            return viewData;
        }

        private IEnumerable<TaskListViewData> GetTagListViewData()
        {
            var tasksByTag = Rtm.GetTasksByTag();
            var taskLists = new List<TaskListViewData>();
            foreach (var tag in tasksByTag.Keys)
            {
                var viewData = new TaskListViewData(tag, string.Format("status:incomplete and tag:{0}", tag));
                viewData.PreLoadTasks(tasksByTag[tag]);
                taskLists.Add(viewData);
            }
            return taskLists;
        }
    }
}
